|
1
|
|
|
/* global API */ |
|
2
|
|
|
var $j = jQuery.noConflict(); |
|
3
|
|
|
|
|
4
|
|
|
$j(document).ready(function () { |
|
5
|
|
|
|
|
6
|
|
|
$j(document).click(function (event) { |
|
7
|
|
|
var passwordPickerRef = '.passwordPickerIframe'; |
|
8
|
|
|
if (!$j(event.target).closest(passwordPickerRef).length) { |
|
9
|
|
|
if ($j(passwordPickerRef).is(":visible")) { |
|
10
|
|
|
removePasswordPicker(); |
|
11
|
|
|
} |
|
12
|
|
|
} |
|
13
|
|
|
}); |
|
14
|
|
|
|
|
15
|
|
|
var _this = this; |
|
16
|
|
|
Array.prototype.findUrl = function (match) { |
|
|
|
|
|
|
17
|
|
|
return this.filter(function (item) { |
|
18
|
|
|
var matchParse = processURL(match, false, false, true, false); |
|
19
|
|
|
return typeof item === 'string' && item.indexOf(matchParse) > -1; |
|
20
|
|
|
}); |
|
21
|
|
|
}; |
|
22
|
|
|
|
|
23
|
|
|
function removePasswordPicker() { |
|
24
|
|
|
activeForm = undefined; |
|
25
|
|
|
$j('.passwordPickerIframe').remove(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
_this.removePasswordPicker = removePasswordPicker; |
|
29
|
|
|
|
|
30
|
|
|
function enterLoginDetails(login, allowSubmit) { |
|
31
|
|
|
var username; |
|
32
|
|
|
|
|
33
|
|
|
if (login.hasOwnProperty('username')) { |
|
34
|
|
|
username = (login.username !== '' ) ? login.username : login.email; |
|
35
|
|
|
} |
|
36
|
|
|
if (!username) { |
|
37
|
|
|
username = null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
fillPassword(username, login.password); |
|
41
|
|
|
|
|
42
|
|
|
if (activeForm) { |
|
43
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: 'isAutoSubmitEnabled'}).then(function (isEnabled) { |
|
44
|
|
|
if (isEnabled && allowSubmit) { |
|
45
|
|
|
submitLoginForm(username); |
|
46
|
|
|
} |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
_this.enterLoginDetails = enterLoginDetails; |
|
52
|
|
|
|
|
53
|
|
|
function enterCustomFields(login, settings) { |
|
54
|
|
|
var customFieldPattern = /^\#(.*)$/; |
|
55
|
|
|
var elementId; |
|
56
|
|
|
var element = false; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/* parhaps wise to try / catch this as this is non essential and no reason to abort previous processing */ |
|
59
|
|
|
try { |
|
60
|
|
|
/* do we have custom_fields for this entry */ |
|
61
|
|
|
if (login.hasOwnProperty('custom_fields') && login.custom_fields.length) { |
|
62
|
|
|
/* yes we do, iterate over all the custom_fields values */ |
|
63
|
|
|
for (var i = 0, len = login.custom_fields.length; i < len; i++) { |
|
64
|
|
|
/* does this custom field label begin with a hash? */ |
|
65
|
|
|
if (customFieldPattern.test(login.custom_fields[i].label)) { |
|
66
|
|
|
/* set variable elementid to whatever element we are trying to auto fill */ |
|
67
|
|
|
elementId = customFieldPattern.exec(login.custom_fields[i].label)[1]; |
|
68
|
|
|
enterCustomFieldElement(elementId, login.custom_fields[i].value); |
|
69
|
|
|
} |
|
70
|
|
|
else if ($j('label[for]:contains(' + login.custom_fields[i].label + ')').length) { |
|
71
|
|
|
elementId = $j('label[for]:contains(' + login.custom_fields[i].label + ')').attr('for'); |
|
72
|
|
|
enterCustomFieldElement(elementId, login.custom_fields[i].value); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
catch (e) { |
|
78
|
|
|
if (settings.debug) { |
|
79
|
|
|
console.log('While attempting to auto fill custom fields the following exception was thrown: ' + e); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
function enterCustomFieldElement(elementId, value) { |
|
85
|
|
|
/* check to see if element id exist */ |
|
86
|
|
|
if ($j('#' + elementId).length) { |
|
87
|
|
|
element = $j('#' + elementId); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
else if ($j('input[name$="' + elementId + '"]').length) { /* maybe element name exist */ |
|
90
|
|
|
element = $j('input[name$="' + elementId + '"]'); |
|
91
|
|
|
} |
|
92
|
|
|
else { /* neither element id or name exist */ |
|
93
|
|
|
element = false; |
|
94
|
|
|
} |
|
95
|
|
|
/* if we have an element and it is type text, number or password, lets auto fill it */ |
|
96
|
|
|
if (element && (element[0].type === 'text' || element[0].type === 'number' || element[0].type === 'password')) { |
|
97
|
|
|
element.val(value); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
function submitLoginForm(username) { |
|
102
|
|
|
if (!activeForm) { |
|
103
|
|
|
// @TODO detect login form on the current page |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
var formEl = $j(activeForm).closest('form'); |
|
108
|
|
|
var iframeUrl = API.extension.getURL('/html/inject/auto_login.html'); |
|
109
|
|
|
$j('#loginPopupIframe').remove(); |
|
110
|
|
|
var loginPopup = $j('<iframe class="loginPopupIframe" scrolling="no" frameborder="0" src="' + iframeUrl + '"></iframe>'); |
|
111
|
|
|
var padding = parseInt($j(formEl).css('padding').replace('px', '')); |
|
112
|
|
|
var margin = parseInt($j(formEl).css('margin').replace('px', '')); |
|
113
|
|
|
var height = Math.round($j(formEl).height() + (padding * 2) + (margin * 2)); |
|
114
|
|
|
var width = Math.round($j(formEl).width() + (padding * 2) + (margin * 2)); |
|
115
|
|
|
loginPopup.attr('height', height); |
|
116
|
|
|
loginPopup.attr('width', width); |
|
117
|
|
|
loginPopup.css('position', 'absolute'); |
|
118
|
|
|
loginPopup.css('z-index', getMaxZ() + 1); |
|
119
|
|
|
loginPopup.css('background-color', 'rgba(0, 0, 0, 0.73)'); |
|
120
|
|
|
loginPopup.css('left', Math.floor($j(formEl).offset().left - padding - margin)); |
|
121
|
|
|
loginPopup.css('top', Math.floor($j(formEl).offset().top - padding - margin)); |
|
122
|
|
|
removePasswordPicker(); |
|
123
|
|
|
$j(document.body).prepend(loginPopup); |
|
124
|
|
|
API.runtime.sendMessage(API.runtime.id, {'setIframeUsername': username}).then(function () { |
|
125
|
|
|
$j(formEl).submit(); |
|
126
|
|
|
setTimeout(function () { |
|
127
|
|
|
loginPopup.remove(); |
|
128
|
|
|
}, 2000); |
|
129
|
|
|
}); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
function getMaxZ() { |
|
133
|
|
|
return Math.max.apply(null, |
|
134
|
|
|
$j.map($j('body *'), function (e) { |
|
135
|
|
|
if ($j(e).css('position') !== 'static') |
|
|
|
|
|
|
136
|
|
|
return parseInt($j(e).css('z-index')) || 1; |
|
|
|
|
|
|
137
|
|
|
})); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
var activeForm; |
|
141
|
|
|
|
|
142
|
|
|
function showPasswordPicker(form) { |
|
143
|
|
|
var jPasswordPicker = $j('.passwordPickerIframe'); |
|
144
|
|
|
if (jPasswordPicker.length > 1) { |
|
145
|
|
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
var loginField = $j(form[0]); |
|
148
|
|
|
var loginFieldPos = loginField.offset(); |
|
149
|
|
|
var loginFieldVisible = loginField.is(':visible'); |
|
150
|
|
|
|
|
151
|
|
|
var position = $j(form[1]).position(); |
|
|
|
|
|
|
152
|
|
|
var passwordField = $j(form[1]); |
|
153
|
|
|
var passwordFieldPos = passwordField.offset(); |
|
154
|
|
|
var passwordFieldVisible = loginField.is(':visible'); |
|
|
|
|
|
|
155
|
|
|
var left = (loginFieldPos) ? loginFieldPos.left : passwordFieldPos.left; |
|
156
|
|
|
var top = (loginFieldPos) ? loginFieldPos.top : passwordFieldPos.top; |
|
157
|
|
|
var maxZ = getMaxZ(); |
|
158
|
|
|
|
|
159
|
|
|
if (passwordFieldPos && loginFieldPos && passwordFieldPos.top > loginFieldPos.top) { |
|
160
|
|
|
//console.log('login fields below each other') |
|
161
|
|
|
top = passwordFieldPos.top + passwordField.height() + 10; |
|
162
|
|
|
} else { |
|
163
|
|
|
// console.log('login fields next to each other') |
|
164
|
|
|
if (loginFieldPos) { |
|
165
|
|
|
top = top + loginField.height() + 10; |
|
166
|
|
|
} else { |
|
167
|
|
|
top = top + passwordField.height() + 10; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
if (!loginFieldVisible) { |
|
171
|
|
|
left = passwordFieldPos.left; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
var pickerUrl = API.extension.getURL('/html/inject/password_picker.html'); |
|
175
|
|
|
|
|
176
|
|
|
var picker = $j('<iframe class="passwordPickerIframe" scrolling="no" height="385" width="350" frameborder="0" src="' + pickerUrl + '"></iframe>'); |
|
177
|
|
|
picker.css('position', 'absolute'); |
|
178
|
|
|
picker.css('left', left); |
|
179
|
|
|
picker.css('z-index', maxZ + 10); |
|
180
|
|
|
picker.css('top', top); |
|
181
|
|
|
$j('body').prepend($j(picker)); |
|
182
|
|
|
activeForm = form; |
|
183
|
|
|
// picker.css('width', $j(form).width()); |
|
184
|
|
|
$j('.passwordPickerIframe:not(:last)').remove(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
function onFormIconClick(e) { |
|
188
|
|
|
e.preventDefault(); |
|
189
|
|
|
e.stopPropagation(); |
|
190
|
|
|
var offsetX = e.offsetX; |
|
191
|
|
|
var offsetRight = (e.data.width - offsetX); |
|
192
|
|
|
if (offsetRight < e.data.height) { |
|
193
|
|
|
showPasswordPicker(e.data.form); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
function createFormIcon(el, form) { |
|
198
|
|
|
var offset = el.offset(); |
|
|
|
|
|
|
199
|
|
|
var width = el.width(); |
|
200
|
|
|
var height = el.height() * 1; |
|
201
|
|
|
var margin = (el.css('margin')) ? parseInt(el.css('margin').replace('px', '')) : 0; |
|
|
|
|
|
|
202
|
|
|
var padding = (el.css('padding')) ? parseInt(el.css('padding').replace('px', '')) : 0; |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
var pickerIcon = API.extension.getURL('/icons/icon.svg'); |
|
205
|
|
|
$j(el).css('background-image', 'url("' + pickerIcon + '")'); |
|
206
|
|
|
$j(el).css('background-repeat', 'no-repeat'); |
|
207
|
|
|
//$j(el).css('background-position', ''); |
|
208
|
|
|
$j(el).css('cssText', el.attr('style') + ' background-position: right 3px center !important;'); |
|
209
|
|
|
|
|
210
|
|
|
$j(el).unbind('click', onFormIconClick); |
|
211
|
|
|
$j(el).click({width: width, height: height, form: form}, onFormIconClick); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
function createPasswordPicker(form) { |
|
215
|
|
|
for (var i = 0; i < form.length; i++) { |
|
216
|
|
|
var el = $j(form[i]); |
|
217
|
|
|
createFormIcon(el, form); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
function formSubmitted(fields) { |
|
222
|
|
|
if (fields[0]) { |
|
223
|
|
|
var user = fields[0].value; |
|
224
|
|
|
} |
|
225
|
|
|
if (fields[1]) { |
|
226
|
|
|
var pass = fields[1].value; |
|
227
|
|
|
} |
|
228
|
|
|
var params = { |
|
229
|
|
|
username: user, |
|
|
|
|
|
|
230
|
|
|
password: pass |
|
|
|
|
|
|
231
|
|
|
}; |
|
232
|
|
|
//Disable password mining |
|
233
|
|
|
//$j(fields[1]).attr('type', 'hidden'); |
|
234
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: "minedForm", args: params}); |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
function inIframe() { |
|
239
|
|
|
try { |
|
240
|
|
|
return window.self !== window.top; |
|
241
|
|
|
} catch (e) { |
|
242
|
|
|
return true; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
function showDoorhanger(data) { |
|
247
|
|
|
if (inIframe()) { |
|
248
|
|
|
return; |
|
249
|
|
|
} |
|
250
|
|
|
data.data.currentLocation = window.location.href; |
|
251
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: "setDoorhangerData", args: data}); |
|
252
|
|
|
var pickerUrl = API.extension.getURL('/html/inject/doorhanger.html'); |
|
253
|
|
|
|
|
254
|
|
|
var doorhanger = $j('<iframe id="password-toolbarIframe" style="display: none;" scrolling="no" height="60" width="100%" frameborder="0" src="' + pickerUrl + '"></iframe>'); |
|
255
|
|
|
$j('#password-toolbarIframe').remove(); |
|
256
|
|
|
doorhanger.css('z-index', getMaxZ() + 1); |
|
257
|
|
|
$j('body').prepend(doorhanger); |
|
258
|
|
|
$j('#password-toolbarIframe').fadeIn(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
_this.showDoorhanger = showDoorhanger; |
|
262
|
|
|
|
|
263
|
|
|
function showUrlUpdateDoorhanger(data) { |
|
264
|
|
|
var buttons = ['cancel', 'updateUrl']; |
|
265
|
|
|
showDoorhanger({ |
|
266
|
|
|
data: data.data, |
|
267
|
|
|
buttons: buttons |
|
268
|
|
|
}); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
_this.showUrlUpdateDoorhanger = showUrlUpdateDoorhanger; |
|
272
|
|
|
|
|
273
|
|
|
function checkForMined() { |
|
274
|
|
|
if (inIframe()) { |
|
275
|
|
|
return; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: "getMinedData"}).then(function (data) { |
|
279
|
|
|
if (!data) { |
|
280
|
|
|
return; |
|
281
|
|
|
} |
|
282
|
|
|
if (data.hasOwnProperty('username') && data.hasOwnProperty('password') && data.hasOwnProperty('url')) { |
|
283
|
|
|
var buttons = ['cancel', 'ignore', 'save']; |
|
284
|
|
|
showDoorhanger({data: data, buttons: buttons}); |
|
285
|
|
|
} |
|
286
|
|
|
}); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
function closeDoorhanger() { |
|
291
|
|
|
$j('#password-toolbarIframe').hide(400); |
|
292
|
|
|
$j('#password-toolbarIframe').remove(); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
_this.closeDoorhanger = closeDoorhanger; |
|
296
|
|
|
|
|
297
|
|
|
function initForms() { |
|
298
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: 'getRuntimeSettings'}).then(function (settings) { |
|
299
|
|
|
var enablePasswordPicker = settings.enablePasswordPicker; |
|
300
|
|
|
var url = window.location.href; |
|
301
|
|
|
var loginFields = getLoginFields(); |
|
302
|
|
|
if (!settings.hasOwnProperty('ignored_sites') || settings.ignored_sites.findUrl(url).length !== 0) { |
|
303
|
|
|
return; |
|
304
|
|
|
} |
|
305
|
|
|
if (loginFields.length > 0) { |
|
306
|
|
|
for (var i = 0; i < loginFields.length; i++) { |
|
307
|
|
|
var form = getFormFromElement(loginFields[i][0]); |
|
308
|
|
|
if(form) { |
|
309
|
|
|
form.setAttribute('autocomplete', 'off'); |
|
310
|
|
|
} |
|
311
|
|
|
if (enablePasswordPicker) { |
|
312
|
|
|
createPasswordPicker(loginFields[i], form); |
|
|
|
|
|
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
//Password miner |
|
316
|
|
|
/* jshint ignore:start */ |
|
317
|
|
|
$j(form).submit((function (loginFields) { |
|
318
|
|
|
return function () { |
|
319
|
|
|
formSubmitted(loginFields); |
|
320
|
|
|
}; |
|
321
|
|
|
})(loginFields[i])); |
|
322
|
|
|
/* jshint ignore:end */ |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
API.runtime.sendMessage(API.runtime.id, { |
|
326
|
|
|
method: "getCredentialsByUrl", |
|
327
|
|
|
args: url |
|
328
|
|
|
}).then(function (logins) { |
|
329
|
|
|
console.log('Found ' + logins.length + ' logins for this site'); |
|
|
|
|
|
|
330
|
|
|
if (logins.length === 1) { |
|
331
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: 'isAutoFillEnabled'}).then(function (isEnabled) { |
|
332
|
|
|
if (isEnabled) { |
|
333
|
|
|
enterLoginDetails(logins[0], false); |
|
334
|
|
|
} |
|
335
|
|
|
}); |
|
336
|
|
|
} |
|
337
|
|
|
}); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
API.runtime.sendMessage(API.runtime.id, { |
|
341
|
|
|
method: "getCredentialsByUrl", |
|
342
|
|
|
args: url |
|
343
|
|
|
}).then(function (logins) { |
|
344
|
|
|
if (logins.length === 1) { |
|
345
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: 'isAutoFillEnabled'}).then(function (isEnabled) { |
|
346
|
|
|
if (isEnabled) { |
|
347
|
|
|
enterCustomFields(logins[0], settings); |
|
348
|
|
|
} |
|
349
|
|
|
}); |
|
350
|
|
|
} |
|
351
|
|
|
}); |
|
352
|
|
|
|
|
353
|
|
|
}); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
function minedLoginSaved(args) { |
|
357
|
|
|
// If the login added by the user then this is true |
|
358
|
|
|
if (args.selfAdded) { |
|
359
|
|
|
showDoorhanger({ |
|
360
|
|
|
data: args, |
|
361
|
|
|
buttons: ['cancel'] |
|
362
|
|
|
}); |
|
363
|
|
|
enterLoginDetails(args.credential, false); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
_this.minedLoginSaved = minedLoginSaved; |
|
368
|
|
|
|
|
369
|
|
|
function resizeIframe(height) { |
|
370
|
|
|
$j('#password-toolbarIframe').height(60 + height); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
_this.resizeIframe = resizeIframe; |
|
374
|
|
|
|
|
375
|
|
|
function copyText(text) { |
|
376
|
|
|
var txtToCopy = document.createElement('input'); |
|
377
|
|
|
txtToCopy.style.left = '-300px'; |
|
378
|
|
|
txtToCopy.style.position = 'absolute'; |
|
379
|
|
|
txtToCopy.value = text; |
|
380
|
|
|
document.body.appendChild(txtToCopy); |
|
381
|
|
|
txtToCopy.select(); |
|
382
|
|
|
document.execCommand('copy'); |
|
383
|
|
|
txtToCopy.parentNode.removeChild(txtToCopy); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
_this.copyText = copyText; |
|
387
|
|
|
|
|
388
|
|
|
function init() { |
|
389
|
|
|
checkForMined(); |
|
390
|
|
|
initForms(); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
var readyStateCheckInterval = setInterval(function () { |
|
394
|
|
|
if (document.readyState === "complete") { |
|
395
|
|
|
clearInterval(readyStateCheckInterval); |
|
396
|
|
|
API.runtime.sendMessage(API.runtime.id, {method: 'getMasterPasswordSet'}).then(function (result) { |
|
397
|
|
|
if (result) { |
|
398
|
|
|
init(); |
|
399
|
|
|
var body = document.getElementsByTagName('body')[0]; |
|
400
|
|
|
if (body) { |
|
401
|
|
|
observeDOM(body, initForms); |
|
402
|
|
|
} |
|
403
|
|
|
} else { |
|
404
|
|
|
console.log('[Passman extension] Stopping, vault key not set'); |
|
|
|
|
|
|
405
|
|
|
} |
|
406
|
|
|
}); |
|
407
|
|
|
} |
|
408
|
|
|
}, 10); |
|
409
|
|
|
|
|
410
|
|
|
API.runtime.onMessage.addListener(function (msg, sender) { |
|
411
|
|
|
//console.log('Method call', msg.method); |
|
412
|
|
|
if (_this[msg.method]) { |
|
413
|
|
|
_this[msg.method](msg.args, sender); |
|
414
|
|
|
} |
|
415
|
|
|
}); |
|
416
|
|
|
}); |
|
417
|
|
|
|